<template>
	<view class="wrap">
		<!-- 主体评论 -->
		<view class="comment">
			<view class="top">
				<view class="left">
					<view class="heart-photo">
						<image :src="imageUrl(comment.avatar)" @click.stop="jumpUserHome(comment.userId)"></image>
					</view>
					<view class="user-info">
						<view class="name">{{ comment.userName }}</view>
						<view class="date">{{comment.createTime|getPublishTime}}</view>
					</view>
				</view>
			</view>
			<view class="content">{{ comment.content }}</view>
			<view class="right" :class="{ highlight: comment.isLike }">
				<!-- <view class="reply" @click="reply(comment)">回复</view> -->
				<u-icon v-if="!comment.isLike" name="thumb-up" class="like" color="#9a9a9a" :size="30"
					@click="getLike"></u-icon>
				<u-icon v-if="comment.isLike" name="thumb-up-fill" class="like" :size="30" @click="getLike"></u-icon>
				<view class="num">{{ comment.likeNumber }}</view>
				<u-icon class="dislike" name="thumb-down" :size="30"></u-icon>
			</view>
		</view>
		<!-- 全部评论 -->
		<view class="all-reply">
			<view class="all-reply-top">全部回复({{ comment.childList.length }})</view>
			<view :class="comment.childList.length>index+1?'item':''" v-for="(item, index) in comment.childList"
				:key="index">
				<view class="comment">
					<view class="top">
						<view class="left">
							<view class="heart-photo" @click.stop="jumpUserHome(item.userId)">
								<image :src="imageUrl(item.avatar)" mode=""></image>
							</view>
							<view class="user-info">
								<view class="name">{{ item.userName }}</view>
								<view class="date">{{ item.createTime|getPublishTime }}</view>
							</view>
						</view>
						<view class="right">
							<view @click.stop="handelAction(item)" class="more-dot">
								<u-icon name="more-dot-fill" color="#999"></u-icon>
							</view>
						</view>
					</view>
					<view class="content">{{ item.content }}</view>
					<!-- 回复评论 -->
					<view class="reply" v-for="(item2,index2) in item.childList" :key="index2">
						<view class="comment" style="background-color: rgb(242, 242, 242);">
							<view class="top">
								<view class="left">
									<view class="heart-photo" @click.stop="jumpUserHome(item2.userId)">
										<image :src="imageUrl(item2.avatar)"></image>
									</view>
									<view class="user-info">
										<view class="name">{{item2.userName }}</view>
										<view class="date">{{item2.createTime|getPublishTime}}</view>
									</view>
								</view>
								<view class="right">
									<view @click.stop="handelAction(item2)" class="more-dot">
										<u-icon name="more-dot-fill" color="#999"></u-icon>
									</view>
								</view>
							</view>
							<view class="content">{{ item2.content }}</view>
							<view class="right" :class="{ highlight: item2.isLike }">
								<u-icon v-if="!item2.isLike" name="thumb-up" class="like" color="#9a9a9a" :size="30"
									@click="replyCommentLike(index,index2)"></u-icon>
								<u-icon v-if="item2.isLike" name="thumb-up-fill" class="like" :size="30"
								@click="replyCommentLike(index,index2)"></u-icon>
								<view class="num">{{ item2.likeNumber }}</view>
								<u-icon class="dislike" name="thumb-down" :size="30" @click=""></u-icon>
							</view>
						</view>
					</view>
					<view class="right" :class="{ highlight: item.isLike }">
						<view class="reply" style="background-color: white" @click="reply(item)">回复</view>
						<u-icon v-if="!item.isLike" name="thumb-up" class="like" :size="30" color="#9a9a9a"
							@click="getLike(index)"></u-icon>
						<u-icon v-if="item.isLike" name="thumb-up-fill" class="like" :size="30"
							@click="getLike(index)"></u-icon>
						<view class="num">{{ item.likeNumber }}</view>
						<u-icon class="dislike" name="thumb-down" :size="30"></u-icon>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: "CommentDetail",
		props: {
			comment: {
				type: Object
			}
		},
		data() {
			return {
				actionComment: undefined
			};
		},
		filters: {
			getPublishTime(dateString) {
				return uni.$u.timeFrom(new Date(dateString), false)
			},
		},
		created() {},
		methods: {
			deleteComment() {
				this.comment.childList.splice(this.comment.childList
					.findIndex(item => item.id === this.actionComment.id), 1)
			},
			jumpUserHome(uid) {
				uni.navigateTo({
					url: "/pages/platform/user/user-space?uid=" + uid
				})
			},
			handelAction(item) {
				this.actionComment = item
				this.$emit('handelAction', item, 'detail')
			},
			replyDetailComment() {
				this.$emit('reply', this.comment)
			},
			reply(comment) {
				this.$emit('reply', comment)
			},
			imageUrl(itemUrl) {
				return this.handleUrl(itemUrl)
			},
			replyCommentLike(comentIndex,index){
				let userLike = {
					itemId: undefined
				}
				userLike.itemId=this.comment.childList[comentIndex].childList[index].id
				this.$u.api.commentData.likeComment(userLike).then(res => {
					if (res.data) {
						this.comment.childList[comentIndex].childList[index].isLike =
						!this.comment.childList[comentIndex].childList[index].isLike;
						
						if (this.comment.childList[comentIndex].childList[index].isLike == true) {
							this.comment.childList[comentIndex].childList[index].likeNumber++;
						} else {
							this.comment.childList[comentIndex].childList[index].likeNumber--;
						}	
					}
					})
				
			},
			// 点赞
			getLike(index) {
				let userLike = {
					itemId: undefined
				}
				if (index === 0 || index > 0) {
					userLike.itemId = this.comment.childList[index].id
					this.$u.api.commentData.likeComment(userLike).then(res => {
						if (res.data) {
							this.comment.childList[index].isLike = !this.comment.childList[index].isLike;
							if (this.comment.childList[index].isLike == true) {
								this.comment.childList[index].likeNumber++;
							} else {
								this.comment.childList[index].likeNumber--;
							}
						}
					})
				} else {
					userLike.itemId = this.comment.id
					this.$u.api.commentData.likeComment(userLike).then(res => {
						if (res.data) {
							if (this.comment.isLike == true) {
								this.comment.isLike = !this.comment.isLike;
								this.comment.likeNumber--;
							} else {
								this.comment.isLike = !this.comment.isLike;
								this.comment.likeNumber++;
							}
						}
					})

				}
			},
		}
	};
</script>

<style lang="scss" scoped>
	.comment {
		padding: 30rpx;
		font-size: 32rpx;
		background-color: #ffffff;

		.top {
			display: flex;
			justify-content: space-between;
		}

		.left {
			display: flex;

			.heart-photo {
				image {
					width: 64rpx;
					height: 64rpx;
					border-radius: 50%;
					background-color: #f2f2f2;
				}
			}

			.user-info {
				margin-left: 10rpx;

				.name {
					color: #5677fc;
					font-size: 28rpx;
					margin-bottom: 4rpx;
				}

				.date {
					font-size: 20rpx;
					color: $u-light-color;
				}
			}
		}

		.right {
			display: flex;
			font-size: 20rpx;
			justify-content: flex-end;
			align-items: center;
			color: #9a9a9a;

			.reply {
				color: #5677fc;
				margin-right: 20rpx;
			}

			.like {
				margin-left: 10rpx;
			}

			.dislike {
				color: #9a9a9a;
				vertical-align: baseline;
			}

			.num {
				margin-left: 5rpx;
				margin-right: 25rpx;
				font-size: 26rpx;
				color: #9a9a9a;
			}
		}

		.content {
			margin-left: 75rpx;
		}

		.highlight {
			color: #00aaff;

			.num {
				color: #00aaff;
			}
		}
	}

	.all-reply {
		margin-top: 10rpx;
		padding-top: 20rpx;
		background-color: #ffffff;

		.all-reply-top {
			margin-left: 20rpx;
			padding-left: 20rpx;
			border-left: solid 4rpx #5677fc;
			font-size: 30rpx;
			font-weight: bold;
		}

		.item {
			border-bottom: solid 2rpx $u-border-color;
		}

		.reply {
			padding: 20rpx;
			background-color: rgb(242, 242, 242);
			border-radius: 12rpx;
			margin: 10rpx 0;

			.username {
				font-size: 24rpx;
				color: #7a7a7a;
			}
		}


	}
</style>